package com.bi.design.mqtt.borker.store.subscribe;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 订阅存储服务
 */
@Slf4j
@Component
public class SubscribeStoreService implements ISubscribeStoreService {

    // 非通配订阅


    private static Map<String, List<SubscribeStore>> subscribeNotWildcardClientSubCache = new ConcurrentHashMap();

    private static List<SubscribeStore> notWildcardSubscribeStores = new ArrayList<>();
    // 通配订阅

    private static Map<String, List<SubscribeStore>> subscribeWildcardClientSubCache = new ConcurrentHashMap();

    private static List<SubscribeStore> wildcardSubscribeStores = new ArrayList<>();

    @Override
    public void put(String topicFilter, SubscribeStore subscribeStore) {
        if (StrUtil.contains(topicFilter, '#') || StrUtil.contains(topicFilter, '+')) {
            wildcardSubscribeStores.add(subscribeStore);
            subscribeWildcardClientSubCache.put(topicFilter, wildcardSubscribeStores);
        } else {
            notWildcardSubscribeStores.add(subscribeStore);
            subscribeNotWildcardClientSubCache.put(topicFilter, notWildcardSubscribeStores);
        }
        log.info("SubscribeStore topic:=>{},subscribeNotWildcardClientSubCache的大小:{},subscribeWildcardClientSubCache的大小:{}", topicFilter, subscribeNotWildcardClientSubCache.size(), subscribeWildcardClientSubCache.size());
    }

    @Override
    public void remove(String topicFilter, String clientId) {
        if (StrUtil.contains(topicFilter, '#') || StrUtil.contains(topicFilter, '+')) {
            subscribeWildcardClientSubCache.remove(topicFilter);
        } else {
            subscribeWildcardClientSubCache.remove(topicFilter);
        }
    }

    @Override
    public void removeForClient(String clientId) {
        // todo
    }

    @Override
    public List<SubscribeStore> search(String topic) {
        List<SubscribeStore> subscribeStores = new ArrayList<SubscribeStore>();
        List<SubscribeStore> list = subscribeNotWildcardClientSubCache.get(topic);
        log.info("search >>  subscribeNotWildcardClientSubCache的大小:{}", subscribeNotWildcardClientSubCache == null ? null : subscribeNotWildcardClientSubCache.size());
        if (list != null && list.size() > 0) {
            subscribeStores.addAll(list);
        }
        subscribeWildcardClientSubCache.forEach((topicFilter, map) -> {
            if (StrUtil.split(topic, '/').size() >= StrUtil.split(topicFilter, '/').size()) {
                List<String> splitTopics = StrUtil.split(topic, '/');//a
                List<String> spliteTopicFilters = StrUtil.split(topicFilter, '/');//#
                String newTopicFilter = "";
                for (int i = 0; i < spliteTopicFilters.size(); i++) {
                    String value = spliteTopicFilters.get(i);
                    if (value.equals("+")) {
                        newTopicFilter = newTopicFilter + "+/";
                    } else if (value.equals("#")) {
                        newTopicFilter = newTopicFilter + "#/";
                        break;
                    } else {
                        newTopicFilter = newTopicFilter + splitTopics.get(i) + "/";
                    }
                }
                newTopicFilter = StrUtil.removeSuffix(newTopicFilter, "/");
                if (topicFilter.equals(newTopicFilter)) {
                    List<SubscribeStore> list2 = new ArrayList<SubscribeStore>(map);
                    subscribeStores.addAll(list2);
                }
            }
        });
        return subscribeStores;
    }
}
